#include "videofactory.h"

#include "bsitedownloaddialog.h"
#include "utils.h"

VideoFactory::VideoFactory(QWidget *parent) : QWidget(parent) {
    m_gridLayout = new QGridLayout(this);
    QPushButton *bsiteDownload = new QPushButton("B站视频下载", this);
    m_gridLayout->addWidget(bsiteDownload, 0, 0);
    connect(bsiteDownload, &QPushButton::clicked, this, [=]() {
        BsiteDownloadDialog dlg;
        dlg.exec();
    });
    QPushButton *subtitle = new QPushButton("字幕获取", this);
    m_gridLayout->addWidget(subtitle, 0, 1);
    connect(subtitle, &QPushButton::clicked, [=]() {
        QDialog *subtitle = new QDialog(this);
        subtitle->resize(0.75 * width(), 0.75 * height());
        QFormLayout *form = new QFormLayout(subtitle);
        QLineEdit *lineedit = new QLineEdit(subtitle);
        lineedit->setPlaceholderText("url or bv");
        QPushButton *pb_start = new QPushButton("crawl", subtitle);
        form->addRow(lineedit);
        form->addRow(pb_start);
        subtitle->open();
    });

    QPushButton *videozip = new QPushButton("视频压缩", this);
    m_gridLayout->addWidget(videozip, 1, 0);
    connect(videozip, &QPushButton::clicked, [=]() {});

    QPushButton *getFrame = new QPushButton("视频帧提取", this);
    m_gridLayout->addWidget(getFrame, 1, 1);
    connect(getFrame, &QPushButton::clicked, [=]() {});

    QPushButton *getaudio = new QPushButton("音频提取", this);
    m_gridLayout->addWidget(getaudio, 1, 2);
    connect(getaudio, &QPushButton::clicked, [=]() {
        QFileDialog dlg;
        dlg.setFileMode(QFileDialog::ExistingFile);
        dlg.setNameFilter("视频文件(*.mp4 *.ts *.avi)");
        if (dlg.exec()) {
            auto filepath = dlg.selectedFiles().at(0);
            auto outfile = filepath + ".aac";
            qDebug() << outfile;
            // ffmpeg -i input-video.avi -vn -acodec copy output-audio.aac
            QString cmd = QString("ffmpeg -i %1 -y -vn -acodec copy %2")
                              .arg(filepath)
                              .arg(outfile);
            runCmd(cmd);
        }
    });

    QPushButton *transfer = new QPushButton("video-transfer", this);
    m_gridLayout->addWidget(transfer, 2, 0);
    connect(transfer, &QPushButton::clicked, this, [=]() {
        QDialog dlg;

        QGridLayout grid(&dlg);

        QPushButton srcFile("video-path", &dlg);
        grid.addWidget(&srcFile, 0, 0);
        QString filePath = "";
        connect(&srcFile, &QPushButton::clicked, [&filePath]() {
            QFileDialog dlg;
            dlg.setFileMode(QFileDialog::ExistingFile);
            dlg.setNameFilter("视频文件(*.mp4 *.ts *.avi *.mkv)");
            if (dlg.exec()) {
                filePath = dlg.selectedFiles().at(0);
            }
        });

        QComboBox dstFormat(&dlg);
        dstFormat.addItems({"mp4"});
        grid.addWidget(&dstFormat, 0, 1);

        QPushButton cancel("Cancel", &dlg);
        QPushButton ok("Ok", &dlg);
        grid.addWidget(&cancel, 1, 0);
        grid.addWidget(&ok, 1, 1);
        connect(&ok, &QPushButton::clicked, [&]() {
            qDebug() << "filePath: " << filePath;
            if (filePath.isEmpty()) {
                return;
            }
            QString filename = QFileInfo(filePath).fileName();
            QString dst = dstFormat.currentText();
            // ffmpeg -i "%1" -vcodec h264 -acodec aac "%1.mp4"
            QString cmd = QString("ffmpg -i %1 -vcodec h264 -acodec aac %2.%3")
                              .arg(filePath)
                              .arg(filename)
                              .arg(dst);
            runCmd(cmd);
        });

        int ret = dlg.exec();
    });
}
